
package com.be.beadmin.service.impl;

import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.service.entity.NotifyInfo;
import com.be.beadmin.service.entity.NotifyInfo_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.service.mapper.NotifyInfoMapperEx;
import com.be.beadmin.service.repository.NotifyInfoRepository;
import com.be.beadmin.service.service.NotifyInfoService;
import com.be.beadmin.service.dto.input.NotifyInfoDto;
import com.be.beadmin.service.dto.output.NotifyInfoEx;
import com.be.beadmin.service.dto.input.NotifyInfoQueryCriteria;
import com.be.beadmin.service.mapper.NotifyInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author ljh
* @date 2023-09-20
**/
@Service
@RequiredArgsConstructor
public class NotifyInfoServiceImpl implements NotifyInfoService {

    @Autowired
    private  NotifyInfoRepository notifyInfoRepository;
    @Autowired
    private  NotifyInfoMapper notifyInfoMapper;
    @Autowired
    private  NotifyInfoMapperEx notifyInfoExMapper;

    @Override
    public Page<NotifyInfoEx> getNotifyInfoByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<NotifyInfo> page = notifyInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(NotifyInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(NotifyInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<NotifyInfoEx> page1 = page.map(notifyInfoExMapper::toEntity);
            return page1;

    }

    @Override
    public void addNotifyInfo(NotifyInfoDto notifyInfoDto) {
            NotifyInfo result = notifyInfoRepository.findNotifyInfoByIdAndDelete(notifyInfoDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("NotifyInfo不存在,请重新查询");
            }
            NotifyInfo notifyInfo = notifyInfoMapper.toEntity(notifyInfoDto);
            notifyInfoRepository.save(notifyInfo);
    }

    @Override
    public void updateNotifyInfo(NotifyInfoDto notifyInfoDto) {
            NotifyInfo notifyInfo = notifyInfoRepository.findNotifyInfoById(notifyInfoDto.getId());
            notifyInfo = notifyInfoMapper.toEntity(notifyInfoDto);
            notifyInfoRepository.save(notifyInfo);
    }


    @Override
    public void delNotifyInfo(String id) {
        NotifyInfo notifyInfo = notifyInfoRepository.findNotifyInfoById(id);
        notifyInfo.setDelete(1);
        notifyInfoRepository.save(notifyInfo);
    }

    @Override
    public void delNotifyInfos (List<String> ids) { }
}